package june.chatbase;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatBaseActivity extends Activity {
	
	private ArrayList<ChatItem> arr_chatItem;
	private ChatBaseAdapter chatAdapter;
	private ListView listView;
	private EditText et;
	private Button bt_left;
	private Button bt_right;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        arr_chatItem = new ArrayList<ChatItem>();
        listView = (ListView)findViewById(R.id.listView1);
        et = (EditText)findViewById(R.id.send_msg);
        bt_left = (Button)findViewById(R.id.l_send);
        bt_right = (Button)findViewById(R.id.r_send);
        
        chatAdapter = new ChatBaseAdapter(ChatBaseActivity.this, arr_chatItem);
        
        listView.setAdapter(chatAdapter);
        
        bt_left.setOnClickListener(onClick);
        bt_right.setOnClickListener(onClick);
    }
    
    /**
     * 통짜 갈아끼기.
     * @param arr_chatItem
     */
    public void setListItem(ArrayList<ChatItem> arr_chatItem){
    	chatAdapter.changeItem(arr_chatItem);
    }
    
    /**
     * 아이템 하나만 만들어서 리스트에 추가.
     * 이를위해 텍스트배열과 방향값을 받는다.
     * @param text
     * @param gravity
     */
    public void addListItem(String[] text, int gravity){
    	
    	ChatItem chatItem = new ChatItem();
    	chatItem.ChatText_1 = text[0];
    	chatItem.ChatText_2 = text[1];
    	chatItem.gravity = gravity;
    	
    	chatAdapter.addChatItem(chatItem);
    }
    
    OnClickListener onClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.l_send:
				addListItem(new String[]{et.getText().toString(), "2"}, Gravity.LEFT);
				break;
			case R.id.r_send:
				addListItem(new String[]{et.getText().toString(), "2"}, Gravity.RIGHT);
				break;
			default:
				break;
			}
		}
	}; 
}